package com.book.upms.user.controller;

import com.book.common.api.core.RESTfulMethod;
import com.book.common.api.entity.CurrentUser;
import com.book.common.result.Failed;
import com.book.common.result.RestResponse;
import com.book.common.result.Success;
import com.book.common.util.RequestInfoUtil;
import com.book.modules.annotation.InjectionCurrentUser;
import com.book.modules.core.controller.BaseController;
import com.book.modules.util.AddressUtil;
import com.book.oauth.server.user.hystrix.IUserHystrix;
import com.book.oauth.server.user.vo.CurrentUserDetail;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;

/**
 * Description：
 *
 * @Author： leo.xiong
 * @CreateDate： 2020/04/28 15:29
 * @Email： leo.xiong@suyun360.com
 * Version：1.0
 */
@Slf4j
@RestController
@Api(value = "当前用户操作")
@RequestMapping(value = "${adminPath}")
public class CurrentUserController extends BaseController {

    @Autowired
    private IUserHystrix userHystrix;

    @PostMapping("/getUserByToken")
    @ResponseBody
    @ApiOperation(value = "根据token获取用户信息", httpMethod = RESTfulMethod.POST, response = Success.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token")
    })
    public RestResponse getUserByToken(@RequestParam(value = "token", required = false) String token) {
        token = StringUtils.isEmpty(token) ? currentUserHelper.getRequestToken() : token;
        return currentUserHelper.getUserByToken(token);
    }

    @PostMapping("/user/detail")
    @ResponseBody
    @InjectionCurrentUser
    @ApiOperation(value = "获取用户的详情信息", httpMethod = RESTfulMethod.POST, response = Success.class)
    public RestResponse getUserDetailByToken(HttpServletRequest request, @ApiIgnore CurrentUser currentUser) {
        if (currentUser == null || StringUtils.isEmpty(currentUser.getId())) {
            return new Failed("BOOK:CORE:10009", i18nHelper.get("BOOK:CORE:10009"));
        }
        CurrentUserDetail currentUserDetail = currentUserHelper.getCurrentUserDetail(currentUser);
        if (currentUserDetail == null) {
            return new Failed("BOOK:CORE:10004", i18nHelper.get("BOOK:CORE:10004"));
        }
        String ip = RequestInfoUtil.getIpAddress(request);
        String location = AddressUtil.getCityInfo(ip);
        String systemInfo = RequestInfoUtil.getSystem(request);
        String browser = RequestInfoUtil.getBrowser(request);
        userHystrix.updateLoginTime(currentUser.getId(), currentUser.getUsername(), location, ip, browser, systemInfo);
        return new Success(currentUserDetail);
    }

    @PostMapping("/reset/password")
    @ResponseBody
    @InjectionCurrentUser
    @ApiOperation(value = "重置密码", httpMethod = RESTfulMethod.POST, response = Success.class)
    public RestResponse resetPassword(@ApiIgnore CurrentUser currentUser) {
        if (currentUser == null || StringUtils.isEmpty(currentUser.getId())) {
            return new Failed("BOOK:CORE:10009", i18nHelper.get("BOOK:CORE:10009"));
        }
        return userHystrix.resetPassword(Long.valueOf(currentUser.getId()));
    }

    @PostMapping("/update/password")
    @ResponseBody
    @InjectionCurrentUser
    @ApiOperation(value = "修改密码", httpMethod = RESTfulMethod.POST, response = Success.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "oldPassword", value = "老密码", required = true),
            @ApiImplicitParam(name = "password", value = "新密码", required = true)
    })
    public RestResponse updatePassword(@RequestParam(value = "oldPassword") String oldPassword, @RequestParam(value = "password") String password, @ApiIgnore CurrentUser currentUser) {
        if (currentUser == null || StringUtils.isEmpty(currentUser.getId())) {
            return new Failed("BOOK:CORE:10009", i18nHelper.get("BOOK:CORE:10009"));
        }
        return userHystrix.updatePassword(Long.valueOf(currentUser.getId()), oldPassword, password);
    }

    @PostMapping("/update/avatar")
    @ResponseBody
    @InjectionCurrentUser
    @ApiOperation(value = "修改头像", httpMethod = RESTfulMethod.POST, response = Success.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "avatar", value = "头像", required = true)
    })
    public RestResponse updateAvatar(@RequestParam(value = "avatar") String avatar, @ApiIgnore CurrentUser currentUser) {
        if (currentUser == null || StringUtils.isEmpty(currentUser.getId())) {
            return new Failed("BOOK:CORE:10009", i18nHelper.get("BOOK:CORE:10009"));
        }
        return userHystrix.updateAvatar(Long.valueOf(currentUser.getId()), avatar);
    }

    @PostMapping("/update/theme")
    @ResponseBody
    @InjectionCurrentUser
    @ApiOperation(value = "修改主题", httpMethod = RESTfulMethod.POST, response = Success.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "theme", value = "主题", required = true)
    })
    public RestResponse updateTheme(@RequestParam(value = "theme") String theme, @ApiIgnore CurrentUser currentUser) {
        if (currentUser == null || StringUtils.isEmpty(currentUser.getId())) {
            return new Failed("BOOK:CORE:10009", i18nHelper.get("BOOK:CORE:10009"));
        }
        return userHystrix.updateTheme(Long.valueOf(currentUser.getId()), theme);
    }

    @PostMapping("/update/tab")
    @ResponseBody
    @InjectionCurrentUser
    @ApiOperation(value = "修改主题", httpMethod = RESTfulMethod.POST, response = Success.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "isTab", value = "是否开启tab：默认:false", required = true, defaultValue = "false")
    })
    public RestResponse updateTab(@RequestParam(value = "isTab") String isTab, @ApiIgnore CurrentUser currentUser) {
        if (currentUser == null || StringUtils.isEmpty(currentUser.getId())) {
            return new Failed("BOOK:CORE:10009", i18nHelper.get("BOOK:CORE:10009"));
        }
        return userHystrix.updateTab(Long.valueOf(currentUser.getId()), isTab);
    }
}
